(function( window, undefined ) {
var $ccb = function (id) {
	return "string" == typeof id ? document.getElementById(id) : id;
};

var Extend = function(destination, source) {
	for (var property in source) {
		destination[property] = source[property];
	}
	return destination;
};

var CurrentStyle = function(element){
	return element.currentStyle || document.defaultView.getComputedStyle(element, null);
};

var Bind = function(object, fun) {
	var args = Array.prototype.slice.call(arguments).slice(2);
	return function() {
		return fun.apply(object, args.concat(Array.prototype.slice.call(arguments)));
	}
};

var Tween = {
	Quart: {
		easeOut: function(t,b,c,d){
			return -c * ((t=t/d-1)*t*t*t - 1) + b;
		}
	},
	Back: {
		easeOut: function(t,b,c,d,s){
			if (s == undefined) s = 1.70158;
			return c*((t=t/d-1)*t*((s+1)*t + s) + 1) + b;
		}
	},
	Bounce: {
		easeOut: function(t,b,c,d){
			if ((t/=d) < (1/2.75)) {
				return c*(7.5625*t*t) + b;
			} else if (t < (2/2.75)) {
				return c*(7.5625*(t-=(1.5/2.75))*t + .75) + b;
			} else if (t < (2.5/2.75)) {
				return c*(7.5625*(t-=(2.25/2.75))*t + .9375) + b;
			} else {
				return c*(7.5625*(t-=(2.625/2.75))*t + .984375) + b;
			}
		}
	}
};

//容器对象,滑动对象,切换数量
var SlideTrans = function(container, slider, count, options) {
	this._slider = $ccb(slider);
	this._container = $ccb(container);//容器对象
	this._timer = null;//定时器
	//this._count = Math.abs(count);//切换数量
	this._target = 0;//目标值
	this._t = this._b = this._c = 0;//tween参数
	this._count = 0;
	
	this.Index = 0;//当前索引
	
	this.SetOptions(options);
	
	this.Auto = !!this.options.Auto;
	this.Duration = Math.abs(this.options.Duration);
	this.Time = Math.abs(this.options.Time);
	this.Pause = Math.abs(this.options.Pause);
	this.Tween = this.options.Tween;
	this.onStart = this.options.onStart;
	this.onFinish = this.options.onFinish;
	this.Warp = this.options.Warp;
	this.isLevelTwo = this.options.isLevelTwo;
	
	var bVertical = !!this.options.Vertical;
	this._css = bVertical ? "top" : "left";//方向
	
	if(this._slider == null || this._container == null){
		return;
	}
	
	this._containerSpace = this._container[this.options.Vertical ? "offsetHeight" : "offsetWidth"];
	this._sliderSpace = this._slider[this.options.Vertical ? "offsetHeight" : "offsetWidth"];
	
	//样式设置
	var p = CurrentStyle(this._container).position;
	p == "relative" || p == "absolute" || (this._container.style.position = "relative");
	this._container.style.overflow = "hidden";
	this._slider.style.position = "absolute";
	this.init(count);
};
SlideTrans.prototype = {
  init: function(count){
		this.Change = this.options.Change ? this.options.Change : (Math.abs(count) ? this._sliderSpace /this._count : this._containerSpace);
		
		if(this._sliderSpace > this._containerSpace){
			this._count = Math.ceil(this._sliderSpace / this.Change);
			if(this.isLevelTwo){
				this.Change = this._containerSpace - 300;
				this._count = Math.ceil((this._sliderSpace - this._containerSpace) / this.Change) + 1;
			}
		}else{
			this._count = Math.abs(count) || Math.ceil(this._sliderSpace / this._containerSpace);
		}
  },
	
  //设置默认属性
  SetOptions: function(options) {
	this.options = {//默认值
		Vertical:	true,//是否垂直方向（方向不能改）
		Auto:		true,//是否自动
		Change:		0,//改变量
		Duration:	50,//滑动持续时间
		Time:		10,//滑动延时
		Pause:		2000,//停顿时间(Auto为true时有效)
		onStart:	function(){},//开始转换时执行
		onFinish:	function(){},//完成转换时执行
		Tween:		Tween.Quart.easeOut,//tween算子
		Warp:		true,//是否循环
		isLevelTwo:	false//对二级菜单做特殊处理
	};
	Extend(this.options, options || {});
  },
  //开始切换
  Run: function(index, isImmediately) {
	//修正index
	index == undefined && (index = this.Index);
	if(this.Warp){
		index < 0 && (index = this._count - 1) || index >= this._count && (index = 0);
	}else{
		//index < 0 && (index = 0) || index >= this._count && (index = this._count - 1);
		if(index < 0){
			this.Index = 0
			return;
		}
		if(index >= this._count){
			this.Index = this._count - 1;
			return;
		}
	}
	//设置参数
	this._target = -Math.abs(this.Change) * (this.Index = index);
	if(this.isLevelTwo){
		var realMove = 0;
		var wantMove = this.Change * this.Index;
		var tds = this._slider.rows[0].cells;
		for(var i=0; i < tds.length - 1; i++){
			if(wantMove <= realMove){
				break;
			}
			realMove = realMove + tds[i].offsetWidth;
			if( realMove < wantMove && wantMove < realMove + tds[i+1].offsetWidth){
				if(! ((index == this._count - 1) && (this._sliderSpace - realMove) > this._containerSpace) ){
					this._target = - realMove;
				}
				break;
			}
		}
	}
	
	this._t = 0;
	this._b = parseInt(CurrentStyle(this._slider)[this.options.Vertical ? "top" : "left"]);
	if(isNaN(this._b)){
		this._b=0;
	}
	this._c = this._target - this._b;
	
	this.onStart();
	this.Move(isImmediately);
  },
  //移动
  Move: function(isImmediately) {
	clearTimeout(this._timer);
	//未到达目标继续移动否则进行下一次滑动
	if (this._c && this._t < this.Duration && isImmediately != true) {
		this.MoveTo(Math.round(this.Tween(this._t++, this._b, this._c, this.Duration)));
		this._timer = setTimeout(Bind(this, this.Move), this.Time);
	}else{
		this.MoveTo(this._target);
		this.Auto && (this._timer = setTimeout(Bind(this, this.Next), this.Pause));
	}
  },
  //移动到
  MoveTo: function(i) {
	this._slider.style[this._css] = i + "px";
  },
  //下一个
  Next: function() {
	this.Run(++this.Index);
  },
  //上一个
  Previous: function() {
	this.Run(--this.Index);
  },

  //停止
  Stop: function() {
	clearTimeout(this._timer); this.MoveTo(this._target);
  }
};

var Map = function(){
	this.buf = new Object();
}
Map.prototype = {
	put: function(key,value){
		this.buf[key] = value;
	},
	
	get: function(key){
		if(this.buf[key]){
			return this.buf[key];
		} else{
			return null;
		}
	},
	
	remove: function(key){
		if(this.buf[key]){
			delete this.buf[key];
		}
	},
	
	cut: function(key){
		if(this.buf[key]){
			var result = this.buf[key];
			delete this.buf[key];
			return result;
		} else{
			return;
		}
	},
	
	size: function(){
		var i = 0;
		for(var ele in this.buf){
			i++;
		}
		return i;
	},
	
	toArray: function(){
		var tmpArray = new Array();
		for(var ele in this.buf){
			tmpArray[tmpArray.length] = ele;
		}
		return tmpArray;
	},
	
	index: function(i){
		var j = 0;
		for(var ele in this.buf){
			if(j == i)
				return this.buf[ele];
			else
				j++;
		}
	}
}
var taskBar=function(){
	this._bar = null;
	this._wall = null;
	this._maxCount = 5;
	this._taskBtns = new Array();
	this._length = 0;
	this._currBtn = null;
	this._wins = new Map();
	this._barPername = "task_";
	this._winPername = "win_";
	this._timer = window.setInterval(Bind(this, this.resizeWin), 500);
};
taskBar.prototype = {
	init: function(bar, wall, maxCount){
		this._bar = $ccb(bar);
		this._wall = $ccb(wall);
		this._maxCount = maxCount || 5;
	},
	
	createWin: function(id, name, src){
		try{
			jQuery.fx.off = true;
			if($ccb(this._barPername + id)){
				this.switchWin($ccb(this._barPername + id));
//				this.submitWin(src,id);
				this.beManipulated();
				jQuery.fx.off = false;
				return;
			}
			var strm=$ccb(id).innerHTML;
			var id1=id;
			var strm1=strm;
			for(var i=0; i<4; i++){
				try{
					var nodeid1 = $ccb(id1).parentElement.parentElement.parentElement;
					//alert(nodeid1.nodeName + "|" + nodeid1.id + "|" + nodeid1.className + "|");
					if(nodeid1.nodeName == "TBODY"){
						nodeid1 = nodeid1.parentElement;
					}
					id1 = nodeid1.id.substr(2);
					var strtmp = $ccb(id1).innerText.replace(/>/,"").replace(/\s/,"");
					if(strtmp != null && strtmp != ""){
						strm1 = strtmp + " > " + strm1;
					}
				}catch(e){
					break;
				}
			}
			if(this._length >= this._maxCount){
				//alert("最多只能打开五个窗口!");
				//return;
				this.closeWin(this._taskBtns[0], true);
			}

			var taskBtn = this._taskBtns[this._length++] = document.createElement("div");
			taskBtn.setAttribute("menuNav",strm1);
			taskBtn.className = "taskOn_div";
			taskBtn.id = this._barPername + id;
			var htmlStr = "<div class='taskOnL_div' id='taskL_" + id + "'></div>";
			htmlStr += "<div class='taskOnM_div' id='taskM_" + id + "'><span>" + name + "</span><a id='taskMa_" + id + "' href='javascript:void(0);'></a></div>";
			htmlStr += "<div class='taskOnR_div' id='taskR_" + id + "'></div>";
			taskBtn.innerHTML = htmlStr;
			this._bar.appendChild(taskBtn);
			taskBtn.onclick = Bind(this,this.switchWin,taskBtn);
			$ccb("taskMa_" + id).onclick = Bind(this,this.closeWin,taskBtn);
			
			var tmpWin = "<iframe src='javascript:return false;' id='"+ this._winPername + id + "' width='100%' frameborder='0' name='"+ this._winPername + id + "' scrolling='auto' onload='this.setAttribute(\"isReady\",\"true\");this.style.height=\"200px\";scrollTo(0,0);'></iframe>"
			var safe = this._wall.ownerDocument.createDocumentFragment();
			var tmp = safe.appendChild(this._wall.ownerDocument.createElement("div"));
			tmp.innerHTML = tmpWin;
			tmp = tmp.lastChild;
			this._wall.appendChild(tmp);
			//$(this._wall).append(tmpWin);
			var taskWin = $ccb(this._winPername + id);
			taskWin.setAttribute("isReady","false");
			this._wins.put(this._barPername + id, taskWin);
			this.submitWin(src,id);
			this.switchWin(taskBtn);
			this.showCloseIcon();
			this.beManipulated();
			taskBtn.setAttribute("isNewWin", "true");
			jQuery.fx.off = false;
		}catch(e){
			alert("createWin:" + e);
		}
	},
	
	switchWin: function(onBtn){
		try{
			if(!onBtn || this._currBtn == onBtn){
				return;
			}
			if(this._currBtn != null){
				this._currBtn.className = "taskOff_div";
				var currId = this._currBtn.id.substr(5);
				$ccb("taskL_" + currId).className = "taskOffL_div";
				$ccb("taskM_" + currId).className = "taskOffM_div";
				$ccb("taskR_" + currId).className = "taskOffR_div";
			}
			if(onBtn != null){
				onBtn.className = "taskOn_div";
				var newId = onBtn.id.substr(5);
				$ccb("taskL_" + newId).className = "taskOnL_div";
				$ccb("taskM_" + newId).className = "taskOnM_div";
				$ccb("taskR_" + newId).className = "taskOnR_div";
			}
			if(this._currBtn == null){
				//$("#win_"+ newId).fadeIn("normal");
				$ccb("win_"+ newId).style.display ="";
			}else{
				//$("#win_"+ currId).fadeOut("normal", function (){$("#win_"+ newId).fadeIn("normal");});
				$ccb("win_"+ currId).style.display ="none";
				$ccb("win_"+ newId).style.display ="";
			}
			this._currBtn = onBtn;
			if($$B.ie6){
				scrollTo(0,0);
			}
			
			if("NO" != this._currBtn.getAttribute("Manipulated")){
				if(frames["win_"+ newId].window.killgoBack && typeof(frames["win_"+ newId].window.killgoBack) ==  "function"){
					frames["win_"+ newId].window.killgoBack();
				}
			}
		}catch(e){
			alert("switchWin:" + e);
		}
	},
	
	closeWin: function(onBtn, isNotJump){
		try{
			for(var i=0; i < this._length; i++){
				if(this._taskBtns[i] == onBtn){
					this._taskBtns.splice(i, 1);
					this._length--;
					var win = this._wins.get(onBtn.id);
					this._wall.removeChild(win);
					this._wins.remove(onBtn.id);
					onBtn.onclick = function(){};
					this._bar.removeChild(onBtn);
					var isCurrBtn = false;
					if(onBtn == this._currBtn){
						this._currBtn = null;
						isCurrBtn = true;
					}
					
					if(this._taskBtns[0] && isNotJump != true && isCurrBtn)
						this.switchWin(this._taskBtns[0]);
					break;
				}
			}
			this.showCloseIcon();
		}catch(e){
			alert("closeWin:" + e);
		}
	},
	resizeWin: function(){
		try{
			var size = this._wins.size();
			for(var i=0; i<size; i++){
				var win = this._wins.index(i);
				var isReady = win.getAttribute("isReady");
				if(win.style.display == "" && isReady == "true"){
					var curHeight = parseInt(CurrentStyle(win)['height']);
					var realWinHeight = curHeight;

					if(win.contentWindow && win.contentWindow.document){
						var elemScrollHeight = win.contentWindow.document.documentElement.scrollHeight || 200;
						var bodyScrollHeight = win.contentWindow.document.body.scrollHeight || 200;
						realWinHeight = elemScrollHeight > bodyScrollHeight ? elemScrollHeight : bodyScrollHeight;
						
					}else if($$B.ie6 || $$B.ie7){
						realWinHeight = win.document.documentElement.scrollHeight || win.document.body.scrollHeight || 200;
					}
					
					if(!isNaN(curHeight) && (curHeight < realWinHeight)){
						win.style.height = realWinHeight + 20 + "px";
					}
				}
			}
		}catch(e){
			//alert("resizeWin:" + e);
		}
	},
	submitWin: function(src,id){
		document.jhform.action =  src;
		document.jhform.target=this._winPername+ id;
		document.jhform.submit();
	},
	
	nextWin: function (){
		if(this._length <= 1) return;
		for(var i=0; i < this._length; i++){
			if(this._taskBtns[i] === this._currBtn){
				var j = i + 1;
				if(j >= this._length){
					j = 0;
				}
				this.switchWin(this._taskBtns[j]);
				break;
			}
		}
		
	},
	
	getMenuNav: function(){
		if(this._currBtn == null) return "";
		
		return this._currBtn.getAttribute("menuNav");
		
	},
	
	isNewWin: function(){
		if(this._currBtn == null) return "";
		
		return this._currBtn.getAttribute("isNewWin");
	},
	
	setOldWin: function(){
		if(this._currBtn == null) return "";
		
		return this._currBtn.setAttribute("isNewWin", "false");
	},
	
	beManipulated: function(){
		if(this._length < 1) return;
		for(var i=0; i < this._length; i++){
			if(this._taskBtns[i] === this._currBtn){
				this._taskBtns[i].setAttribute("Manipulated", "NO");
			}else{
				this._taskBtns[i].setAttribute("Manipulated", "OFF");
			}
		}
	},
	
	showCloseIcon: function(){
		try{
			for(var i=0; i < this._length; i++){
				var id = this._taskBtns[i].id.substr(5);
				if(this._length > 1){
					$ccb("taskMa_" + id).style.display = "block";
				}else{
					$ccb("taskMa_" + id).style.display = "none";
				}
				
			}
		}catch(e){
			alert("showCloseIcon:" + e);
		}
	}
	
};

$$B = (function(ua){
	var b = {
		msie: /msie/.test(ua) && !/opera/.test(ua),
		opera: /opera/.test(ua),
		safari: /webkit/.test(ua) && !/chrome/.test(ua),
		firefox: /firefox/.test(ua),
		chrome: /chrome/.test(ua)
	};
	var vMark = "";
	for (var i in b) {
		if (b[i]) { vMark = "safari" == i ? "version" : i; break; }
	}
	b.version = vMark && RegExp("(?:" + vMark + ")[\\/: ]([\\d.]+)").test(ua) ? RegExp.$1 : "0";
	
	b.ie = b.msie;
	b.ie6 = b.msie && parseInt(b.version, 10) == 6;
	b.ie7 = b.msie && parseInt(b.version, 10) == 7;
	b.ie8 = b.msie && parseInt(b.version, 10) == 8;
	
	return b;
})(window.navigator.userAgent.toLowerCase());

var RepairFixed = function() {
	if ( !$$B.ie6 ) return {"append": function(){}, "remove": function(){}};
	var layer, body, parent = "__repairfixed";//记录父节点
	function Create(){//创建定位层函数
		body = document.body
		if (body.currentStyle.backgroundAttachment !== "fixed") {
			if (body.currentStyle.backgroundImage === "none") {
				body.runtimeStyle.backgroundRepeat = "no-repeat";
				body.runtimeStyle.backgroundImage = "url(none)";
			}
			body.runtimeStyle.backgroundAttachment = "fixed";
		}
		layer = document.createElement("<div style='position:absolute;z-index:1;border:0;padding:0;margin:0;overflow:hidden;background:transparent;top:expression((document).documentElement.scrollTop);left:expression((document).documentElement.scrollLeft);width:expression((document).documentElement.clientWidth);height:expression((document).documentElement.clientHeight);display:block;filter:alpha(opacity=100);'>");
		Create = function(){};
	}
	return {
		"append": function(elem){
			Create();
			elem[ parent ] = elem.parentNode;
			layer.appendChild(elem).runtimeStyle.position = "absolute";
			//插入body
			if ( layer.parentNode !== body ) body.insertBefore(layer, body.childNodes[0]);
		},
		"remove": function(elem){
			if ( !layer ) return;
			//移除元素
			if ( elem.parentNode === layer ) {
				elem.runtimeStyle.position = "";
				elem[ parent ] ? elem[parent].appendChild(elem) : layer.removeChild(elem);
				elem.removeAttribute(parent);//不能用delete
			}
			//没有内部元素就移除
			if ( !layer.childNodes.length && layer.parentNode == body ) body.removeChild(layer);
		}
	};
}();
RepairFixed.append($ccb("taskbarbg"));
RepairFixed.append($ccb("sidebar"));
//RepairFixed.append($ccb("realtime_tips"));

var CCBMenu = function(parentId, childId, vertical, paretCCBMenu, brotherCCBMenus){
	this.$parent = $('#'+parentId);
	this.$child = $('#'+childId);
	this._timer = null;
	this._openTimer = null;
	this._vertical = vertical;
	this._childCloseDelay = 100;
	this._parentOpenDelay = 0;
	this._childOpenDelay  = 200;
	this._paretCCBMenu = paretCCBMenu;
	this._brotherCCBMenus = brotherCCBMenus;
	this.colsechild = function(){
		this._timer = setTimeout(Bind(this,function(){
			this.$child.hide();
			var $el = this.$child;
			$el
			.find("ul")
			.css({ top: 0 })
			.end()
			.find("a")
		}), this._childCloseDelay);
		if(this._paretCCBMenu != null){
			this.$parent.removeClass("dropdown_a_hover");
		}
	}
	
	this.clearTimer = function(){
		clearTimeout(this._timer);
		this._timer = null;
		if(paretCCBMenu != null && this._paretCCBMenu != undefined){
			this._paretCCBMenu.clearTimer();
		}
		if(this._paretCCBMenu != null){
			this.$parent.addClass("dropdown_a_hover");
		}
	}

	this.$parent.mouseover(Bind(this,function(){
		var timeOut = this._vertical ? this._parentOpenDelay : this._childOpenDelay;
		this._openTimer = setTimeout(Bind(this, function(){

			this.clearTimer();
			for(var i=0; i < this._brotherCCBMenus.length; i++){
				this._brotherCCBMenus[i].$child.hide();
				this._brotherCCBMenus[i].$child.find("a").each(function(){
					var mId = $(this).attr("id");
					var drmId = 'dr'+mId;
					if($ccb(drmId) == null) return;
					$("#"+drmId).hide();
				});
			}
			this.$child.show();
			if(this.$child.width() < 120)
				this.$child.width(120);
				
			if ($$B.ie6 || $$B.ie7){
				var maxWidth = 120;
				this.$child.find("a").each(function(){
					var textWidth = $(this).text().length * 16 + 25;
					maxWidth = maxWidth > textWidth ? maxWidth : textWidth;
				});
				this.$child.width(maxWidth);
			}
			
			if(this._vertical){
				this.$child.offset({left:this.$parent.offset().left, top:this.$parent.offset().top + this.$parent.height()});
			}else{
				if(this.$parent.offset().left + this.$parent.width() + this.$child.width() + 25 > $(document).width()){
					this.$child.offset({left:this.$parent.offset().left - this.$child.width() - 2, top:this.$parent.offset().top});
				}else{
					this.$child.offset({left:this.$parent.offset().left + this.$parent.width() + 25, top:this.$parent.offset().top});
				}
			}
	
			var $hoverObj = this.$parent;
			var $container = this.$child;
			var containerId = $container.attr("id");
			var hasMoreId = "hm" + containerId.substr(2, containerId.length);
			var $list = $container.find("ul");
			var heightLi = $container.find("li").height();
			var maxHeight = (heightLi+1) * 10;
			var heightUl = $list.height();
			if(heightUl - maxHeight < heightLi)
				return;
	
			var multiplier = (heightUl + heightLi * 2) / maxHeight;		
	
			if (multiplier > 1) {
				
				$("#"+hasMoreId).show();
				$container
					.css({
						height: maxHeight,
						overflow: "hidden"
					})
					.mousemove(function(e) {
						var offset = $container.offset();
						var relativeY = ((e.pageY - offset.top) * (multiplier - 1));
						if (relativeY > heightLi) {
							$list.css("top", -relativeY + heightLi);
						}else{
							$list.css("top",0);
						}
					});
			}
		}), timeOut);
	}))
	.mouseleave(Bind(this,function(){
		this.colsechild();
		clearTimeout(this._openTimer);
	}));
	
	this.$child.mouseover(Bind(this,function(){
		this.clearTimer();
	}))
	.mouseleave(Bind(this,function(){
		this.colsechild();
		if(paretCCBMenu != null && this._paretCCBMenu != undefined){
			this._paretCCBMenu.colsechild();
		}
	}));
}
window.jsonExtend 	= Extend;
window.CurrentStyle = CurrentStyle;
window.Bind 		= Bind;
window.Tween 		= Tween;
window.Slider		= SlideTrans;
window.taskBar 		= new taskBar();
window.$$B 			= $$B;
window.CCBMenu 		= CCBMenu;
window.buff			= new Map();
})( window );